package com.loongtech.bi.action.advertisment;

import com.loongtech.bi.advertisment.controller.BaseAdvAction;
import com.loongtech.bi.advertisment.model.PayHourData;
import com.loongtech.bi.advertisment.model.PayMinData;
import com.loongtech.bi.advertisment.model.chart.AdvChartUIData1;
import com.loongtech.bi.advertisment.model.chart.AdvChartUIData2;
import com.loongtech.bi.advertisment.model.chart.AdvChartUIData3;
import com.loongtech.bi.advertisment.model.chart.AdvChartUIData4;
import com.loongtech.bi.advertisment.service.PayUserService;
import com.loongtech.bi.manager.system.SysUserManager;
import com.loongtech.bi.support.chart.ChartTool;
import com.loongtech.bi.support.chart.ChartType;
import com.loongtech.bi.support.chart.ChartUIData;
import com.loongtech.core.util.Time;
import com.loongtech.core.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: PayUserBiAction
 *
 * @author liuchen
 * @Description: 市场数据分析 - 实时：广告位数据
 * @date 2016-11-28 下午1:47:24
 */
@Controller
@Scope("prototype")
@RequestMapping("/market/payUserMarket")
public class RTPayUserBiAction extends BaseAdvAction {

    /**
     *
     */
    private static final long serialVersionUID = 3325353020790731397L;

    @Resource(name = "payUserService")
    private PayUserService payUserService;

    @Autowired
    private SysUserManager sysUserManager;

    @RequestMapping(value = "initParam.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void initParam(HttpServletRequest request, HttpServletResponse response, @RequestParam String uid, String callback) {
        try {
            Map<String, Object> result = new HashMap<>();

            Integer userid = sysUserManager.getUserIdByName(uid);
            if (null == userid) {
                result.put("res", 1);
                result.put("msg", "帐户错误，请联系管理员!");
                printToJson(response, result, callback);
                return;
            }

            initData(userid);

            result.put("res", "0");
            result.put("surl", getId());
            result.put("affiliation", getAffiliation());
            result.put("channelName", getChannelName());
            result.put("agent", getAgent());
            result.put("platform", getPlatform());
            result.put("paytype", getPaytype());
            result.put("advPosition", getAdvPosition());
            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }

    private List<PayHourData> payHourDatas;

    private List<PayMinData> payMinDatas;


    @RequestMapping(value = "search.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void search(HttpServletRequest request, HttpServletResponse response, @RequestParam String day,
                       @RequestParam String agent, @RequestParam String platform, @RequestParam String surl,
                       @RequestParam String affiliation, @RequestParam String channelName, @RequestParam String paytype,
                       @RequestParam String advPosition, @RequestParam String uid, String callback) {
        try {
            Map<String, Object> result = new HashMap<>();

            Integer userid = sysUserManager.getUserIdByName(uid);
            if (null == userid) {
                result.put("res", 1);
                result.put("msg", "帐户错误，请联系管理员!");
                printToJson(response, result, callback);
                return;
            }
            initData(userid);

            if (hasAgents == null || hasAgents.size() <= 0) {
                result.put("res", "1");
                result.put("msg", "此用户无任何代理商权限，请联系管理员配置!");
                printToJson(response, result, callback);
                return;
            }
            /*if ((agent == "" || agent.split(",").length <= 0)) {
				result.put("res", "1");
				result.put("msg", "请选择代理商!");
				printToJson(response, result, callback);
				return;
			}*/


            String[] surls = "".equals(surl) ? getId().toArray(new String[getId().size()]) : Utils.matchesString(surl).split(",");
            String[] affiliations = "".equals(affiliation) ? getAffiliation().toArray(new String[getAffiliation().size()]) : Utils.matchesString(affiliation).split(",");
            String[] channelNames = "".equals(channelName) ? getChannelName().toArray(new String[getChannelName().size()]) : Utils.matchesString(channelName).split(",");
            String[] agents = "".equals(agent) ? getAgent().toArray(new String[getAgent().size()]) : Utils.matchesString(agent).split(",");
            String[] platforms = "".equals(platform) ? getPlatform().toArray(new String[getPlatform().size()]) : Utils.matchesString(platform).split(",");
            String[] paytypes = "".equals(paytype) ? getPaytype().toArray(new String[getPaytype().size()]) : Utils.matchesString(paytype).split(",");
            String[] advPositions = "".equals(advPosition) ? getAdvPosition().toArray(new String[getAdvPosition().size()]) : Utils.matchesString(advPosition).split(",");

            payHourDatas = new ArrayList<>();
            payMinDatas = new ArrayList<>();
            ChartUIData lineChart1 = new ChartUIData();
            ChartUIData lineChart2 = new ChartUIData();
            ChartUIData lineChart3 = new ChartUIData();
            ChartUIData lineChart4 = new ChartUIData();

            payHourDatas = payUserService.getListHourData(day, agents, platforms);
            payMinDatas = payUserService.getListMinData(day, surls, affiliations, channelNames, agents, platforms,
                    paytypes, advPositions);


            String lastday = Time.getDateByHNum(Time.formatDate(day,"yyyy-MM-dd"), -1);
            List<AdvChartUIData1> data1 = payUserService.getAdvActDate(day, lastday, selectedAgent, selectedPlatform);
            if (data1 != null && data1.size() > 0) {
                lineChart1.setJsId("1");
                lineChart1.setJsonType(ChartType.SPLINE.type);
                lineChart1
                        .setJsonStr(new ChartTool(data1, AdvChartUIData1.class, "").loadData(lineChart1.getJsonType()));
            }
            List<AdvChartUIData2> data2 = payUserService.getAdvActMacDate(day, lastday, selectedAgent, selectedPlatform);
            if (data2 != null && data2.size() > 0) {
                lineChart2.setJsId("2");
                lineChart2.setJsonType(ChartType.SPLINE.type);
                lineChart2
                        .setJsonStr(new ChartTool(data2, AdvChartUIData2.class, "").loadData(lineChart2.getJsonType()));
            }
            List<AdvChartUIData3> data3 = payUserService.getAdvActUserDate(day, lastday, selectedAgent, selectedPlatform);
            if (data3 != null && data3.size() > 0) {
                lineChart3.setJsId("3");
                lineChart3.setJsonType(ChartType.SPLINE.type);
                lineChart3
                        .setJsonStr(new ChartTool(data3, AdvChartUIData3.class, "").loadData(lineChart3.getJsonType()));
            }
            List<AdvChartUIData4> data4 = payUserService.getAdvActPayDate(day, lastday, selectedAgent, selectedPlatform);
            if (data4 != null && data4.size() > 0) {
                lineChart4.setJsId("4");
                lineChart4.setJsonType(ChartType.SPLINE.type);
                lineChart4
                        .setJsonStr(new ChartTool(data4, AdvChartUIData4.class, "").loadData(lineChart4.getJsonType()));
            }

            result.put("res", "0");
            result.put("payHourDatas", payHourDatas);
            result.put("payMinDatas", payMinDatas);

            result.put("activeChart", lineChart1);
            result.put("newActChart", lineChart2);
            result.put("newAccountChart", lineChart3);
            result.put("newPayChart", lineChart4);

            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }

    @RequestMapping(value = "searchMin.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void searchMin(HttpServletRequest request, HttpServletResponse response, @RequestParam String day,
                          @RequestParam String agent, @RequestParam String platform, @RequestParam String surl,
                          @RequestParam String affiliation, @RequestParam String channelName, @RequestParam String paytype,
                          @RequestParam String advPosition, @RequestParam String uid, String callback) {
        try {
            Map<String, Object> result = new HashMap<>();

            Integer userid = sysUserManager.getUserIdByName(uid);
            if (null == userid) {
                result.put("res", 1);
                result.put("msg", "帐户错误，请联系管理员!");
                printToJson(response, result, callback);
                return;
            }
            initData(userid);

            if (hasAgents == null || hasAgents.size() <= 0) {
                result.put("res", "1");
                result.put("msg", "此用户无任何代理商权限，请联系管理员配置!");
                printToJson(response, result, callback);
                return;
            }
			/*if ((agent == "" || agent.split(",").length <= 0)) {
				result.put("res", "1");
				result.put("msg", "请选择代理商!");
				printToJson(response, result, callback);
				return;
			}*/

            String[] surls = "".equals(surl) ? getId().toArray(new String[getId().size()]) : Utils.matchesString(surl).split(",");
            String[] affiliations = "".equals(affiliation) ? getAffiliation().toArray(new String[getAffiliation().size()]) : Utils.matchesString(affiliation).split(",");
            String[] channelNames = "".equals(channelName) ? getChannelName().toArray(new String[getChannelName().size()]) : Utils.matchesString(channelName).split(",");
            String[] agents = "".equals(agent) ? getAgent().toArray(new String[getAgent().size()]) : Utils.matchesString(agent).split(",");
            String[] platforms = "".equals(platform) ? getPlatform().toArray(new String[getPlatform().size()]) : Utils.matchesString(platform).split(",");
            String[] paytypes = "".equals(paytype) ? getPaytype().toArray(new String[getPaytype().size()]) : Utils.matchesString(paytype).split(",");
            String[] advPositions = "".equals(advPosition) ? getAdvPosition().toArray(new String[getAdvPosition().size()]) : Utils.matchesString(advPosition).split(",");

            payMinDatas = new ArrayList<>();

            payMinDatas = payUserService.getListMinData(day, surls, affiliations, channelNames, agents, platforms,
                    paytypes, advPositions);
            result.put("res", "0");
            result.put("payMinDatas", payMinDatas);
            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }
}
